package edu.vt.ece_3574.amber_alert.membership;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.MemberMain;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.accounts.Friend;
import edu.vt.ece_3574.amber_alert.messaging.MessagingUtils;

public class HangoutAdderActivity extends Activity{

	/** Called when the activity is first created. */
	int hangoutNum = 1;
	String name_;
	String number_;
	String address_;
	EditText friendName;
	EditText friendNumber;
	EditText friendAddress;

	String fillOut_ = "Please fill out all fields";
	String succHangout_ = "Hangout creation success! Your information was"
			+ " sent like this to your primary:";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hangout_creation);

		Button createHangoutButton = (Button) findViewById(R.id.createHangout);
		Button returntoMemb = (Button) findViewById(R.id.returncreateHangout);
		createHangoutButton.setOnClickListener(onButton);
		returntoMemb.setOnClickListener(onReturn);
}
	private View.OnClickListener onButton = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			friendName = (EditText) findViewById(R.id.friendName);
			friendNumber = (EditText) findViewById(R.id.friendNumber);
			friendAddress = (EditText) findViewById(R.id.friends_address);

			if (friendName != null || friendNumber != null) {
				number_ = friendNumber.getText().toString();
				name_ = friendName.getText().toString();
				address_ = friendAddress.getText().toString();
					switch (v.getId()) {
					case R.id.createHangout: {
						Friend friend = new Friend(name_, number_, address_);
						AmberAlertApplication.get().getCurrentAccount().addFriend(friend);
						
						createHangout();
						Context context = getApplicationContext();
						CharSequence text = succHangout_ + " " + name_ + " " + number_ + " " + address_;
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.setGravity(Gravity.CENTER
								| Gravity.FILL_HORIZONTAL, 0, 0);
						toast.show();
						hangoutNum++;
						}
					}
					}
				 
			 else {
				Context context = getApplicationContext();
				CharSequence text = fillOut_ + "," + name_ + "," + number_ + "," + address_;
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(context, text, duration);
				toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
				toast.show();
			}

		}
	};
	private View.OnClickListener onReturn = new View.OnClickListener() {
		public void onClick(View v) {
		Intent i = new Intent(HangoutAdderActivity.this, MemberMain.class);
		HangoutAdderActivity.this.finish();
		startActivity(i);
		}
	};
	
	public void createHangout() {
		String hangoutMessage = "I am now with " + name_ + " at " + address_ 
				+ " and their number is: " + number_;


		//ArrayList<Account> primaries = ((AmberAlertApplication) getApplicationContext()).getCurrentAccount().getAssociation().getAccounts();
		ArrayList<Account> primaries = AmberAlertApplication.get().getCurrentAccount().getAssociatedAccounts();
		if (primaries.size() == 0) {
			Intent i = new Intent(HangoutAdderActivity.this, MemberAccountSettings.class);
			HangoutAdderActivity.this.finish();
			startActivity(i);
		}
		else {
			for (Account primaryNumber : primaries) {
				MessagingUtils.get().sendSMS(primaryNumber.getNumber(), hangoutMessage);
			}
		}
		//Return to the login screen after creating an account
		Intent i = new Intent(HangoutAdderActivity.this, MemberMain.class);
		HangoutAdderActivity.this.finish();
		startActivity(i);
	}
	
	public void onBackPressed() {
		Intent i = new Intent(HangoutAdderActivity.this, MemberMain.class);
		startActivity(i);
		finish();
		return;
	}
	
}